<?php
class MvItem{
    public $mvname;
    public $mvurl;
}

abstract class InfoSeeker{
    private $mvObject;
    abstract public function run($param);
}
abstract class InfoSeekerName{}
abstract class InfoSeekerUrl{
    private $supportAnalyser = NULL;
    public function isSupportAnalyser($analyserClassName = ''){
        return (is_array($this->supportAnalyser) && in_array($analyserClassName, $this->supportAnalyser))
                || (is_string($this->supportAnalyser) && $this->supportAnalyser == $analyserClassName);
    }
}

abstract class ResourceSeeker{
    private $mvResource;
    
    abstract public function run($param);
}
abstract class ResourceSeekerName{}
abstract class ResourceSeekerUrl{}

abstract class ListAnalyser{
    private $listurl;
    private $infoSeekers = array();
    private $resourceSeeker = array();
    
    public function __constructor($listurl){
        $this->listurl = $listurl;
    }
    
    abstract public function analyse();//return a array of MvItem
    
    public function loadInfoSeeker(InfoSeeker $infoSeeker){
        $this->infoSeekers[] = $infoSeeker;
    }
    
    public function loadResourceSeeker(ResourceSeeker $resourceSeeker){
        $this->resourceSeeker[] = $resourceSeeker;
    }
    
    public function run(){
        $mvItems = $this->analyse();
        if(count($mvItems)){
            foreach($mvItems as $mvitem){
                $mvObject = NULL;
                $mvResource = NULL;
                //1.seek info
                if(count($this->infoSeekers)){
                    foreach($this->infoSeekers as $infoSeeker){
                        
                        if($infoSeeker instanceof InfoSeekerUrl){
                            if($mvitem->mvurl){//for url, do check usable
                                if($infoSeeker->isSupportAnalyser(get_class($this))){
                                    $mvObject = $infoSeeker->run($mvitem->mvurl);
                                }
                            }
                        }elseif($infoSeeker instanceof InfoSeekerName){
                            if($mvitem->mvname)
                                $mvObject = $infoSeeker->run($mvitem->mvname);
                        }
                        if($mvObject !== NULL) break;//only seek one
                    }
                }
                //2.seek resource
                if(count($this->resourceSeeker)){
                    foreach($this->resourceSeeker as $resourceSeeker){
                        
                        if($resourceSeeker instanceof ResourceSeekerUrl){
                            if($mvitem->mvurl)
                                $mvResource = $resourceSeeker->run($mvitem->mvurl);
                        }else{
                            if($mvitem->mvname)
                                $mvResource = $resourceSeeker->run($mvitem->mvname);
                        }
                        if($mvResource !== NULL) break;//only seek one
                    }
                }
                //3.save
                if($mvObject === NULL || !$mvObject->save()){
                    //save a title only
                    $tmp = new MvObject;
                    $tmp->title = $mvitem->mvname;
                }
            }
        }
        return NULL;
    }
}
